/*
 * NodeContainer.h
 *
 *  Created on: 29-Sep-2014
 *      Author: ali
 */

#ifndef NODECONTAINER_H_
#define NODECONTAINER_H_
#include <map>
#include "Node.h"
#include "Constant.h"
class NodeContainer
{
	static std::map<int, Node*> m_mapNode;
	static int m_nElemCount;
public:
	NodeContainer();

	static void PutNode(int key, Node* np)
	{
		Node *pNode = new Node();
		if (NULL == pNode)
		{
			return;
		}
		*pNode = *np;
		m_mapNode[key] = pNode;
		++m_nElemCount;
	}

	static double GetCurrentResidualPower()
	{
		double dResidualPower = 0;
		for (std::map<int, Node*>::const_iterator it = m_mapNode.begin();
				it != m_mapNode.end(); ++it)
		{
			int key = it->first;
			Node *pNode = it->second;
			if (NULL == pNode)
			{
				continue;
			}
			if (pNode->GetResidualPower() < dActivityhreasholdPower)
			{
				//cout << "Node # " << pNode->GetID() << " Residual Power "<< fixed << pNode->GetResidualPower() << endl;
			}
			dResidualPower += pNode->GetResidualPower();
		}
		return dResidualPower;
	}

	static int GetActiveNodeCount()
	{
		return m_nElemCount - GetDeadNodeCount();
	}

	static int GetDeadNodeCount()
	{
		int iDeadNodeCnt = 0;

		for (std::map<int, Node*>::const_iterator it = m_mapNode.begin();
				it != m_mapNode.end(); ++it)
		{
			int key = it->first;
			Node *pNode = it->second;
			if (NULL == pNode)
			{
				continue;
			}

			if (pNode->GetResidualPower() < dActivityhreasholdPower)
			{
				iDeadNodeCnt += 1;
			}
		}
		return iDeadNodeCnt;
	}
	static void DebugPrint()
	{
		for (std::map<int, Node*>::const_iterator it = m_mapNode.begin();
				it != m_mapNode.end(); ++it)
		{
			int key = it->first;
			Node *pNode = it->second;
			if (NULL == pNode)
			{
				continue;
			}
			cout << "Node # " << key << " Residual Power "
					<< pNode->GetResidualPower() << endl;
		}
	}

	static Node* GetNode(int id)
	{
		Node* np = m_mapNode[id];
		return np;
	}

	static void Cleanup()
	{
		for (std::map<int, Node*>::const_iterator it = m_mapNode.begin();
				it != m_mapNode.end(); ++it)
		{
			int key = it->first;
			Node *pNode = it->second;
			delete pNode;
			pNode = NULL;
		}
	}

	~NodeContainer();
};

#endif /* NODECONTAINER_H_ */
